//
//  BVIPinfomiationViewController.m
//  DistributorAB
//
//  Created by 主事丫环 on 2019/6/17.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BVIPinfomiationViewController.h"
#import "BVIPSecTableViewCell.h"
#import "BVIPinfoTableViewCell.h"
#import "BFPViewController.h"

@interface BVIPinfomiationViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *leftArr;
@property (nonatomic, assign) BOOL isfapiao;
@end

@implementation BVIPinfomiationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    
    self.title = @"入会信息";
    self.leftArr = [NSMutableArray arrayWithCapacity:1];
    self.dataSource = [NSMutableArray arrayWithCapacity:1];
    
    NSArray *arr = @[@"会费金额",@"支付方式",@"缴费日期",@"有效期至",@"电子发票"];
    [self.leftArr addObjectsFromArray:arr];
    _isfapiao = NO;
    [self requestData];
}

- (void)requestData {
    
    [self.dataSource removeAllObjects];
    
    kWeakSelf;
    
    [NetWorkTool POST:getpowerInfo param:nil success:^(id dic) {
        NSDictionary *muydic = [dic objectForKey:@"data"];
        KMyLog(@"ruehuwfh%@",muydic);
        //[self.dataSource addObject:[muydic objectForKey:@"pay_money"]];
        NSString *money = [NSString stringWithFormat:@"%@元",[muydic objectForKey:@"pay_money"]];
        [self.dataSource addObject:money];
        [self.dataSource addObject:[muydic objectForKey:@"pay_style"]];
        [self.dataSource addObject:[[muydic objectForKey:@"pay_time"] substringToIndex:10]];
        [self.dataSource addObject:[[muydic objectForKey:@"end_time"] substringToIndex:10]];
        [self.dataSource addObject:@""];
        
        if (strIsEmpty([muydic objectForKey:@"invioceid"])) {
            weakSelf.isfapiao = YES;//没发票
        }
        [self.tableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}

#pragma mark -- tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_isfapiao) {
        return self.dataSource.count-1;
    }
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_isfapiao) {
        BVIPinfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BVIPinfoTableViewCell" forIndexPath:indexPath];
        cell.rightLb.text = [_dataSource safeObjectAtIndex:indexPath.row];
        cell.leftLb.text = [_leftArr safeObjectAtIndex:indexPath.row];
        return cell;
        
    }else{
        BVIPinfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BVIPinfoTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        BVIPSecTableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"BVIPSecTableViewCell" forIndexPath:indexPath];
        cell2.selectionStyle = UITableViewCellSelectionStyleNone;
        if (_dataSource.count-1 == indexPath.row) {
            return cell2;
        }else{
            cell.rightLb.text = [_dataSource safeObjectAtIndex:indexPath.row];
            cell.leftLb.text = [_leftArr safeObjectAtIndex:indexPath.row];
        }
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == (_dataSource.count-1)) {
        BFPViewController *fp = [[BFPViewController alloc]init];
        fp.muarr = _dataSource;
        [self.navigationController pushViewController:fp animated:YES];
    }
}

#pragma mark -- 懒加载
- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNaviHeight, ScreenW, ScreenH-kNaviHeight-5) style:(UITableViewStylePlain)];
        [self.view addSubview:self.tableView];
        _tableView.backgroundColor = [UIColor colorWithHexString:@"eeeeee"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.contentInset = UIEdgeInsetsMake(5, 0, 40, 0);
        if (iOS11) {
            self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.tableFooterView = [UIView new];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 120;
        [_tableView registerNib:[UINib nibWithNibName:@"BVIPSecTableViewCell" bundle:nil] forCellReuseIdentifier:@"BVIPSecTableViewCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"BVIPinfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"BVIPinfoTableViewCell"];
    }
    return _tableView;
}

- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray arrayWithCapacity:1];
    }
    return _dataSource;
}

@end
